module view {
    //铁匠铺
    export class SmithyView extends eui.Component {
        closeBtn: eui.Button
        descGroup: eui.Group
        itemsGroup: eui.Group
        itemNameLabel: eui.Label
        needItemLabel: eui.Label
        progressBar: eui.ProgressBar
        makeItemBtn: eui.Button
        fuseBtn: eui.Button
        selectItemId: number
        fuseProgressBar: eui.ProgressBar
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/SmithySkin.exml"
        }

        private onComplete(): void {
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.progressBar.addEventListener(egret.TouchEvent.TOUCH_TAP,this.workbenchTouch,this)
            this.fuseBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.fuse,this)
            this.fuseProgressBar.addEventListener(egret.TouchEvent.TOUCH_TAP,this.fuseTouch,this)
            this.showSmithyItem()
            this.showWorkbench()
            this.showFuse()
        }
        
        private close(evt: egret.TouchEvent): void {
            this.parent.removeChild(this)
        }

        private workbenchTouch(evt: egret.TouchEvent): void {
            var workbench = gplayer.workbenches.smithy[0]
            if(!workbench) {
                return
            }
            var itemId = workbench.id
            var completeTime = workbench.completeTime
            var getItemId = templateConfig.smithyItem[itemId].getItem
            var nowTime = new Date().getTime()
            if(completeTime <= nowTime) {
                var itemComponent = game.utilsHandle.createItem({
                    id: getItemId,
                    stack: 1
                })
                gplayer.store.addItem(itemComponent)
                gplayer.workbenches.smithy.splice(0,1)
                this.showWorkbench()
            }
        }

        protected showWorkbench() {
            var smithyItem = gplayer.workbenches.smithy[0]
            var p = new MakerItemProgressBar(this.progressBar,smithyItem)
        }

        protected showSmithyItem(): void {
            var firstFlag = true
            for(var i in templateConfig.smithyItem) {
                var item = templateConfig.smithyItem[i]
                var selectButton: eui.RadioButton = new eui.RadioButton();
                selectButton.skinName = 'resource/control/SmithyRadioBtnSkin.exml'
                selectButton.label = item.name
                selectButton.groupName = "smithyGroup"
                selectButton.horizontalCenter = 0
                this.itemsGroup.addChild(selectButton)
                selectButton.value = item.id
                selectButton.addEventListener(eui.UIEvent.CHANGE,this.setItemInfo,this)
                if(firstFlag) {
                    firstFlag = false
                    selectButton.selected = true
                    this.selectItemId = item.id
                    var itemConfig = templateConfig.smithyItem[item.id]
                    this.itemNameLabel.text = itemConfig.name
                    this.needItemLabel.text = ''
                    for(var i in itemConfig.need.items) {
                        var needItem = itemConfig.need.items[i]
                        this.needItemLabel.text += templateName[needItem[0]] + 'X' + needItem[1] + '\n'
                    }
                }
            }
            this.makeItemBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.makeItem,this)
        }
        
        //熔炉
        private fuse(evt: egret.TouchEvent): void{
            var child = new view.FuseView()
            this.stage.addChild(child)
            this.parent.removeChild(this)
        }
        
        private showFuse() {
            if(gplayer.fuse.startTime !== 0 && gplayer.fuse.startTime + 60000 > new Date().getTime() ) {
                var p = new FouseProgressBar(this.fuseProgressBar)
                this.fuseProgressBar.visible = true
                this.fuseBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.fuse,this)
            } else {
                this.fuseProgressBar.visible = false
                this.fuseBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.fuse,this)
            }
            
        }
        
        private fuseTouch(evt: egret.TouchEvent){
            if(gplayer.fuse.startTime !== 0 && gplayer.fuse.startTime + 60000 <= new Date().getTime()) {              
                var item = new game.ItemComponent({
                    id: gplayer.fuse.getItemId,
                    stack: 1
                })
                gplayer.store.addItem(item)
                gplayer.fuse.startTime = 0
                gplayer.fuse.getItemId = 0
                this.fuseProgressBar.visible = false               
            }
            this.showFuse()
        }
        
        private setItemInfo(evt: eui.UIEvent): void {
            var target = evt.target
            var itemId = target.value
            this.selectItemId = itemId
            var itemConfig = templateConfig.smithyItem[itemId]
            this.itemNameLabel.text = itemConfig.name
            this.needItemLabel.text = ''
            for(var i in itemConfig.need.items) {
                var needItem = itemConfig.need.items[i]
                this.needItemLabel.text += templateName[needItem[0]] + 'X' + needItem[1] + '\n'
            }
        }

        private makeItem(evt: egret.TouchEvent): void {
            var smithyItemConfig = templateConfig.smithyItem[this.selectItemId]
            if(gplayer.workbenches.smithy.length >= 1) {
                return
            }
            var ret = game.utilsHandle.removeItemsAndGold(gplayer,smithyItemConfig.need)
            if(ret === -1) {
                return
            }
            var completeTime = new Date().getTime() + smithyItemConfig.duration
            var smithyItem = {
                id: this.selectItemId,
                completeTime: completeTime
            }
            gplayer.workbenches.smithy.push(smithyItem)
            var p = new MakerItemProgressBar(this.progressBar,smithyItem)
        }
    }

    class MakerItemProgressBar {
        progressBar: eui.ProgressBar
        timer: egret.Timer
        smithyItem: any
        itemId: number
        completeTime: number
        duration: number
        public constructor(progressBar: eui.ProgressBar,smithyItem) {
            this.progressBar = progressBar
            this.progressBar.maximum = 1000
            this.progressBar.labelFunction = this.barLabelFunction
            if(!smithyItem) {
                this.progressBar.slideDuration = 0
                this.progressBar.value = 0
                return
            }
            this.smithyItem = smithyItem
            this.itemId = this.smithyItem.id
            this.completeTime = this.smithyItem.completeTime
            this.duration = templateConfig.smithyItem[this.itemId].duration
            this.progressBar['getItemId'] = templateConfig.smithyItem[this.itemId].getItem
            var nowTime = new Date().getTime()
            var per = 100 - (this.completeTime - nowTime) / this.duration * 100
            this.progressBar.value = per * 10
            this.timer = new egret.Timer(200,(this.completeTime - nowTime) / 200 + 1)
            this.timer.addEventListener(egret.TimerEvent.TIMER,this.timerHandler,this)
            this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.timerComFunc,this)
            this.timer.start()
        }

        private barLabelFunction(value: number,maximum: number): string {
            if(value === 0) {
                return '工作台'
            } else if(value === maximum) {
                return templateName[this['getItemId']]
            } else {
                return '制作中... ' + Math.ceil(value / maximum * 100) + '%'
            }
        }

        private timerHandler(evt: egret.TimerEvent): void {
            var nowTime = new Date().getTime()
            if(this.completeTime < nowTime) {
                this.progressBar.value = 1000
                return
            }
            var per = 100 - (this.completeTime - nowTime) / this.duration * 100
            var cd = (this.completeTime - nowTime) / 1000
            this.progressBar.value = per * 10
        }

        private timerComFunc(event: egret.TimerEvent) {
        }
    }
    
    class FouseProgressBar {
        progressBar: eui.ProgressBar
        timer: egret.Timer
        workBenchPet: any
        completeTime: number
        duration: number
        public constructor(progressBar: eui.ProgressBar) {
            this.progressBar = progressBar
            this.progressBar.maximum = 1000
            this.progressBar.labelFunction = this.barLabelFunction
            var nowTime = new Date().getTime()
            var per = 1000 - (gplayer.fuse.startTime + 60000 - nowTime) / 60000 * 1000
            this.progressBar.value = per
            this.timer = new egret.Timer(200,(gplayer.fuse.startTime + 60000 - nowTime) / 200 + 1)
            this.timer.addEventListener(egret.TimerEvent.TIMER,this.timerHandler,this)
            this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.timerComFunc,this)
            this.timer.start()
        }

        private barLabelFunction(value: number,maximum: number): string {
            if(Math.ceil(value / maximum * 100) === 100) {
                return templateName[gplayer.fuse.getItemId]
            } else {
                return '融合中... ' + Math.ceil(value / maximum * 100) + '%'
            }
        }

        private timerHandler(evt: egret.TimerEvent): void {
            var nowTime = new Date().getTime()
            if(gplayer.fuse.startTime + 60000 < nowTime) {
                this.progressBar.value = 1000
                return
            }
            var per = 100 - (gplayer.fuse.startTime + 60000 - nowTime) / 60000 * 100
            this.progressBar.value = per * 10
        }

        private timerComFunc(event: egret.TimerEvent) {
        }
    }
}
